package com.pj.lib.auraServer.components.DB.tables;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import com.pj.lib.auraServer.components.DB.DB;
import com.pj.lib.auraServer.exceptions.AuraServerException;
import com.pj.lib.aura.connection.AuraTypeModel;
import com.pj.lib.auraServer.utils.Log;

public abstract class AuraType implements TableInterface{
	private String TABLE_NAME = "LIB_AURA_TYPES";
	
	public static final String COLUMN_CODE = "CODE";
	public static final String COLUMN_NAME = "NAME";
	public static final String COLUMN_RADIUS= "RADIUS";
	public static final String COLUMN_ACTION_ENTER_CODE = "ACTION_ENTER_CODE";
	public static final String COLUMN_ACTION_TEMP_LEAVE_CODE = "ACTION_TEMP_LEAVE_CODE";
	public static final String COLUMN_ACTION_RETURN_CODE = "ACTION_RETURN_CODE";
	public static final String COLUMN_ACTION_EXIT_CODE = "ACTION_EXIT_CODE";
	public static final String COLUMN_LEAVING_TIME = "LEAVING_TIME";
	
	protected DB db;
	
	@Override
	public String getTableName() {
		return TABLE_NAME;
	}

	@Override
	public void setTableName(String name) {
		TABLE_NAME = name;
	}
	
	@Override
	public String getTableSequence() {
		return null;
	}

	@Override
	public void setTableSequence(String name) {
		
	}

	@Override
	public void delete() throws AuraServerException, SQLException {
		db.exec((new StringBuilder("DROP TABLE "))
				.append(getTableName())
				.append(" IF EXISTS").toString());
	}
	
	@Override
	public void createIfNotExists(ArrayList<Map<String, Object>> dbTables) throws AuraServerException, SQLException {
		Boolean found = false;
		for(Map<String, Object> row : dbTables) {
			if(row.get("TABLENAME").equals(this.getTableName().toLowerCase()))
				found = true;
		}
		if(!found) {
			Log.i(this, "Creating table " + this.getTableName() + "...");
			this.create();
			Log.i(this,  this.getTableName() + " created.");
		}
		
	}
	
	
	@Override
	public void setDB(DB db) {
		this.db = db;
		
	}
	
	

	@Override
	public String getDefaultOrder() {
		return " ORDER BY " + COLUMN_CODE;
	}

	
	public abstract Long add(AuraTypeModel obj) throws AuraServerException, SQLException;
	
	
	
}
